#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 rambla.eu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import os
import platform, os, sys, datetime
import unittest
import setup_environ
import json
import raws_json
from raws_json.raws_service import RawsService, Feed, Query
from raws_json.meta.service import MetaService
from raws_json.rass.service import RassService
from raws_json.meta.meta import *

class ContentdirTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.meta_service = MetaService(username = setup_environ.TEST_USERNAME, password = setup_environ.TEST_PASSWORD, server = setup_environ.TEST_SERVER)
        cls.rass_service = RassService(username = setup_environ.TEST_USERNAME, password = setup_environ.TEST_PASSWORD, server = setup_environ.TEST_RASS_SERVER)
        
        # delete all existing content instances
        feed = cls.meta_service.getContentList()
        mf = Feed(feed)
        for e in mf.entries:
            cls.meta_service.deleteContent(e)
        
        # delete '/meta_unit_test' dir from RASS    
        status = cls.rass_service.delete("/dir/meta_unit_test/?recursive=1")
        print "deleted '/meta_unit_test', status = %s" % str(status)
            
        # make sure files exist on the CDN
        status = cls.rass_service.getItemHeaderFromPath("aaa.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "aaa.mp4", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa.webm")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "aaa.webm", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa_00.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "aaa_00.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa_50.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "aaa_50.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa_100.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "aaa_100.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("bbb.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "bbb.mp4", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("ccc.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "/meta_unit_test", filename = "ccc.mp4", local_path = setup_environ.LOCAL_FILE)

        # create content instances
        af1 = FileObj(path = "/meta_unit_test/aaa.mp4", media_type = "video", duration = 60)
        af2 = FileObj(path = "/meta_unit_test/aaa.webm", media_type = "video", duration = 60)
        af3 = FileObj(path = "/meta_unit_test/aaa_00.jpg", media_type = "image")
        af4 = FileObj(path = "/meta_unit_test/aaa_50.jpg", media_type = "image")
        af5 = FileObj(path = "/meta_unit_test/aaa_100.jpg", media_type = "image")
        am1 = MetaObj(meta_name = "Title", vocab = "dc", text = "Cars", lang = "en")
        am2 = MetaObj(meta_name = "Creator", vocab = "dc", text = "Pixar", lang = "en")
        am3 = MetaObj(meta_name = "Publisher", vocab = "dc", text = "Pixar", lang = "en")
        am4 = MetaObj(meta_name = "Description", vocab = "dc", text = "Cars is a 2006 American computer-animated comedy-adventure film produced by Pixar", lang = "en")
        am5 = MetaObj(meta_name = "Audience", vocab = "dc", text = "6+")
        cls.cars_mc = MetaContent(name = "cars", tags = ["cars", "animated", "movie", "Pixar"], file_objs = [af1,af2,af3,af4,af5], meta_objs = [am1,am2,am3,am4,am5])
        cars_entry = cls.meta_service.createContent(cls.cars_mc.to_entry())

        bf1 = FileObj(path = "/meta_unit_test/bbb.mp4", media_type = "video", duration = 120)
        bm1 = MetaObj(meta_name = "Title", vocab = "dc", text = "Big Buck Bunny", lang = "en")
        bm2 = MetaObj(meta_name = "Creator", vocab = "dc", text = "Blender institute", lang = "en")
        bm3 = MetaObj(meta_name = "Description", vocab = "dc", text = "Big Buck Bunny (code-named Peach) is a short computer animated film by the Blender Institute", lang = "en")
        bm4 = MetaObj(meta_name = "Audience", vocab = "dc", text = "6+")
        cls.big_buck_bunny_mc = MetaContent(name = "big_buck_bunny", tags = ["animated", "movie", "Blender", "python", "BigBuckBunny"], file_objs = [bf1,], meta_objs = [bm1, bm2, bm3,bm4])
        big_buck_bunny_entry = cls.meta_service.createContent(cls.big_buck_bunny_mc.to_entry())

   
    def test_get_contentdir(self):
        """ Get contentdir /meta_unit_test/ """
        feed = self.meta_service.getContentDirList(dirpath = "/meta_unit_test")
        mf = Feed(feed)
        self.assertEqual(3, len(mf.entries))
        for e in mf.entries:
            mc = MetaContent(entry = e)
            if unicode(mc.name) == unicode(self.cars_mc.name):
                self.assertEqual(mc, self.cars_mc)
            elif unicode(mc.name) == unicode(self.big_buck_bunny_mc.name):
                self.assertEqual(mc, self.big_buck_bunny_mc)
            elif not mc.name:
                self.assertEqual(len(mc.file_objs), 1)
                for f in mc.file_objs:
                    self.assertEqual(f.path, "/meta_unit_test/ccc.mp4")
            else:
                self.assertTrue("test_search_meta(): unexpected entry name" + mc.name)


def suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(ContentdirTest))
   return suite

if __name__=='__main__':
    unittest.main()
